package com.zjitc.serviceedu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zjitc.common.exception.AuthException;
import com.zjitc.common.utils.JwtUtils;
import com.zjitc.common.utils.MD5;
import com.zjitc.serviceedu.entity.EduUser;
import com.zjitc.serviceedu.entity.vo.RegisterVo;
import com.zjitc.serviceedu.mapper.EduUserMapper;
import com.zjitc.serviceedu.service.EduUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;
import java.util.WeakHashMap;

/**
 * <p>
 * 会员表 服务实现类
 * </p>
 *
 * @author 赵诚挚
 * @since 2024-10-10
 */
@Service
public class EduUserServiceImpl extends ServiceImpl<EduUserMapper, EduUser> implements EduUserService {

    //登录方法
    @Override
    public String login(EduUser eduUser) {
        String phone = eduUser.getPhone();
        String password = eduUser.getPassword();
        String username = eduUser.getUsername();

        // 判断用户名和密码是否为空
        if (StringUtils.isEmpty(username) || StringUtils.isEmpty(password)) {
            throw new AuthException("用户名或密码不能为空");
        }

        // 查询用户名是否存在
        QueryWrapper<EduUser> wrapper = new QueryWrapper<>();
        wrapper.eq("username", username);
        EduUser selectUserInfo = baseMapper.selectOne(wrapper);

        // 检查用户是否存在
        if (selectUserInfo == null) {
            throw new AuthException("用户名不存在");
        }

        // 检查密码是否正确，建议使用更安全的散列算法替代 MD5
        if (!MD5.encrypt(password).equals(selectUserInfo.getPassword())) {
            throw new AuthException("密码不正确");
        }

        // 检查用户是否被禁用
        if (selectUserInfo.getIsDisabled()) {
            throw new AuthException("用户已被禁用");
        }

        // 生成JWT令牌
        String jwtToken = JwtUtils.getJwtToken(
                String.valueOf(selectUserInfo.getId()),
                selectUserInfo.getNickname(),
                selectUserInfo.getAvatar(),
                selectUserInfo.getPhone()
        );

        return jwtToken;

    }
    //注册方法
    @Override
    @ApiOperation("注册")
    public void register(RegisterVo registerVo) {
        String code = registerVo.getCode();
        String phone = registerVo.getPhone();
        String password = registerVo.getPassword();
        String username = registerVo.getUsername();

        if (StringUtils.isEmpty(username) || StringUtils.isEmpty(password) || StringUtils.isEmpty(code) || StringUtils.isEmpty(phone)) {
            throw new AuthException("注册失败：用户名、密码、验证码或手机号为空");
        }
        //添加数据到数据库
        EduUser eduUser = new EduUser();
        eduUser.setPhone(phone);
        eduUser.setUsername(username);
        eduUser.setPassword(MD5.encrypt(password));
        eduUser.setAvatar("https://th.bing.com/th/id/OIP.RFgnRHK1YvW7-fUS-v8L1AAAAA?w=159&h=176&c=7&r=0&o=5&dpr=1.3&pid=1.7");
        baseMapper.insert(eduUser);

    }
}
